<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


/* 关于返回结果的json格式建议如下：
data:执行成功返回的数据
status:简单的表示业务执行的状态，如成功success，登录失败，更新失败等
code:业务执行状态的代码（可不设计）
message:表示详细信息
version:版本号 
*/


class TaskCalendar extends MY_Controller
{

	//取得所有安排了日程的任务
	public function get_task_for_cal()
	{
		//		$sql=('SELECT id, title, start_date AS start,allday FROM task WHERE start_date IS NOT NULL AND stage<>"finish" AND uid='.$this->_login_userid.';');
		$sql = ('SELECT id, title, start_date AS start,allday FROM task WHERE start_date IS NOT NULL AND archive=0 AND uid=' . $this->_login_userid . ';');
		$this->query_to_json($sql);
	}

	//取得所有安排了日程的任务（针对自定义的日历控件用）
	public function getmycal($theday = '')
	{

		//		$this->load->database();
		//		$this->db->select('id, title, start_date AS start,allday');
		//		$sql=('SELECT id, title, start_date AS start,allday FROM task WHERE start_date IS NOT NULL AND stage<>"finish" AND uid='.$this->_login_userid.';');

		$sql = 'SELECT id, title, start_date AS start,allday,stage FROM task WHERE start_date IS NOT NULL  ';
		if ($this->_login_userid) {
			if (isset($_POST['teamid'])) {
				$sql .= ' AND tid=' . $_POST['teamid'];
			} else {
				$sql .= ' AND uid=' . $this->_login_userid . ' AND tid IS NULL ';
			}
		} else {
			$sql .= ' AND uid=5 AND tid IS NULL ';
		}
		if ($theday) {
			$sql .= ' AND start_date BETWEEN date_sub("' . $theday . '",interval 7 day) AND  date_add("' . $theday . '",interval 42 day)';
		} else {
			$sql .= ' AND start_date BETWEEN date_sub(NOW(),interval 7 day) AND  date_add(NOW(),interval 42 day)';
		}


		//如果设置了根任务参数，则只取根任务的下级任务，使用child_list自定义函数
		if (isset($_POST['root_task'])) {
			$parent = $_POST['root_task'];
			$childlist = $this->get_all_child_list($parent);
			$sql .= ' AND FIND_IN_SET(id,"' . $childlist . '")>0 '; //从子任务列表中取';
		}
		$this->query_to_json($sql);
	}
}
